package demo1;

class Animal{
    public String name;
    public int age;

    public Animal(String name,int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void eat(){

        System.out.println(this.name + "->正在吃！(Animal)");
    }
}

//理论上的设计，里面应该包含猫特有的属性或者方法
class Cat extends Animal{
    public Cat(String name,int age){
        super(name,age);
    }
    @Override//注解：
    public void eat(){
        System.out.println("Cat::eat()");
    }
}
class Bird extends Animal{
    public String swing;
    public Bird(String name, int age) {
        super(name, age);
    }
    public void fly(){
        System.out.println(this.name + "->正在飞！(Animal)");
    }
    @Override//注解：
    public void eat(){
        System.out.println("Bird::eat()");
    }
}

public class TestDemo {


    public static void main(String[] args) {
        Animal animal1 = new Cat("一号", 12);
        if (animal1 instanceof Bird) {
            Bird bird = (Bird) animal1;
            bird.fly();
        }else{
            System.out.println("zxcvbnm");
        }
        System.out.println("============");
        Animal animal2 = new Bird("二号", 66);
        if (animal2 instanceof Bird) {
            Bird bird = (Bird) animal2;
            bird.fly();
        }
    }

    public static void func(Animal animal) {
        animal.eat();
    }
    public static void main5(String[] args) {
        Animal animal1 = new Cat("一号", 12);
        Animal animal2 = new Bird("二号", 66);

        func(animal1);
        func(animal2);
    }

    public static void main4(String[] args) {
        Cat cat = new Cat("一号", 12);
        Bird bird = new Bird("二号", 66);

        func(cat);
        func(bird);
    }

    public static void main3(String[] args) {
        Animal animal1 = new Cat("一号", 12);
        animal1.eat();

        Animal animal2 = new Bird("二号", 66);
        animal2.eat();
        //animal2.fly();  父类只能调用自己的成员
    }

//    public static void func(Animal animal){
//
//    }
    public static Animal func2(){
        int a = 1;
        if (a == 1) {
            return new Cat("一号",12);
        }else {
            return new Bird("二号",66);
        }
    }

    public static void main2(String[] args) {
        Cat cat = new Cat("一号",12);
        func(cat);


        //向上转型->不管猫还是鸟，都是动物，所以，可以拿Animal接受
        Animal animal1 = new Cat("一号",12);

        Animal animal2 = new Bird("二号",66);

    }

    public static void main1(String[] args) {
        Cat cat = new Cat("一号",12);
        cat.eat();

        Bird bird = new Bird("二号",66);
        bird.eat();
        bird.fly();

        Animal animal = new Animal("animal",99);
        animal.eat();
        //animal.fly(); 一个应用调用了哪个方法，需要看当前的引用的类型【站在现在这个角度只能看到这里】
    }
}
